//package com.myproject.mall.gateway.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.myproject.mall.common.constants.RedisConstants;
//import com.myproject.mall.common.result.HttpResult;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilter;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
//@Component
//@Slf4j
//public class TokenGatewayFilter implements GatewayFilter, Ordered {
//
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//
//    @Override
//    @SneakyThrows
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        log.debug("用户网关过滤器……………………");
//
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//
//        String path = request.getURI().getPath();
//        if (path.contains("/api/login") || path.contains("v3/api-docs/") || path.contains("/v3/api-docs")) {
////            放行
//            return chain.filter(exchange);
//        }
//
//        HttpHeaders headers = request.getHeaders();
//        List<String> tokens = headers.get("Authorization");
//
//        ObjectMapper mapper = new ObjectMapper();
//        DataBuffer dataBuffer = null;
//
//        if (tokens == null || tokens.isEmpty()) {
////            异常处理
//            response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//            HttpResult<String> result = HttpResult.fail("token不存在，用户登录异常");
//            dataBuffer = response.bufferFactory().wrap(
//                    mapper.writeValueAsString(result)
//                            .getBytes(StandardCharsets.UTF_8));
//            return response.writeWith(Mono.just(dataBuffer));
//        }
//
//        String token = tokens.get(0);
//        if (!stringRedisTemplate.hasKey(RedisConstants.USER_TOKEN_PREFIX + token)) {
//            response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//            HttpResult<String> result = HttpResult.fail("用户登录异常");
//            dataBuffer = response.bufferFactory().wrap(
//                    mapper.writeValueAsString(result)
//                            .getBytes(StandardCharsets.UTF_8));
//            return response.writeWith(Mono.just(dataBuffer));
//        }
//
//        return chain.filter(exchange);
//    }
//
//    /**
//     * 配置过滤器执行顺序<br/>
//     * 该方法定义了过滤器的执行顺序，getOrder() 返回的值决定了当前过滤器在过滤链中的优先级。数值越小，优先级越高。
//     *
//     * @return int
//     */
//    @Override
//    public int getOrder() {
//        return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 100;
//    }
//}
